package com.byt.netapi.interceptor;

import android.text.TextUtils;

import com.byt.netapi.INetRequestInfo;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 请求过滤器
 */
public class RequestInterceptor implements Interceptor {
    private INetRequestInfo mNetRequestInfo;
    public RequestInterceptor(INetRequestInfo netRequestInfo) {
        this.mNetRequestInfo = netRequestInfo;
    }
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request()
                .newBuilder();
        if(mNetRequestInfo != null) {
            for(String key:mNetRequestInfo.getRequestHeaderMap().keySet()){
                if(!TextUtils.isEmpty(mNetRequestInfo.getRequestHeaderMap().get(key))) {
                    builder.addHeader(key, mNetRequestInfo.getRequestHeaderMap().get(key));
                }
            }
        }
        return chain.proceed(builder.build());
    }
}